<?php

namespace App\Admin\Renderable;

use Dcat\Admin\Grid;
use App\Models\WorkUser;
use App\Enums\WorkTicketStatusEnum;
use Dcat\Admin\Grid\LazyRenderable;

/**
 * @property mixed $input
 * @property mixed $school_id
 * @mixin \App\Models\WorkUser
 */
class WorkUserTable extends LazyRenderable
{
    public function grid(): Grid
    {
        $school_id = $this->school_id;
        $builder = WorkUser::with('tickets')->when($school_id, function ($q) use($school_id) {
            $q->whereHas('schools', function ($query) use ($school_id) {
                $query->where('id', $school_id);
            });
        });
        return Grid::make($builder, function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('real_name','姓名');
            $grid->column('tel','电话');
            $grid->column('enable','状态')->bool();
            $grid->column('memo','备注');
            $grid->column('tickets','未结工单数量')->display(function () {
                return $this->tickets->whereIn('status', [WorkTicketStatusEnum::待处理, WorkTicketStatusEnum::处理中])->count();
            });
            $grid->column('created_at')->format('Y-m-d H:i:s');
            $grid->column('updated_at')->format('Y-m-d H:i:s');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->width(2);
                $filter->like('real_name','姓名')->width(2);
                $filter->like('tel','电话')->width(2);
                $filter->equal('enable','状态')->select([1 => '可用', 0 => '禁用'])->width(2);
                $filter->like('memo','备注')->width(2);
                $filter->where('schools',function ($query) {
                    $query->whereHas('schools', function ($query) {
                        $query->where('name','like', '%'.$this->input.'%');
                    });
                },'学校');
            });
            $grid->disableActions();
            // $grid->enableDialogCreate();
            // $grid->showQuickEditButton();
            // $grid->paginate(5);
            $grid->disablePagination();
        });
    }
}
